/*
 * Copyright (C) 2004-2007  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "stdafx.h"
#include "FdoWcsGetCoverage.h"
#include "FdoWcsXmlGlobals.h"
#include <FdoCommonStringUtil.h>
#include <OWS/FdoOwsGlobals.h>

#ifdef _DEBUG
#include <fstream>
using namespace std;
#endif

#define STRING_FORMAT_BUFFER_LENGTH (100)
 
FdoWcsGetCoverage::FdoWcsGetCoverage (FdoString* identifier, FdoString* format, 
                                                FdoString* srsName, 
                                                FdoSize height,
									            FdoSize width,
		                                        FdoDouble minx, 
		                                        FdoDouble miny, 
		                                        FdoDouble maxx, 
		                                        FdoDouble maxy,
                                      FdoBoolean store,
                                      FdoWcsGridCRS* gridCRS,
                                      FdoWcsTimeSequence* timeSequence,
                                      /*TODO: RangeSubSet*/
                                      FdoString* version)
: FdoOwsRequest(FdoWcsXmlGlobals::WCSServiceName, FdoWcsXmlGlobals::WcsGetCoverageRequest),
        mSrsName(srsName), 
        mFormat(format),
        mHeight(height),
		mWidth(width),
        mMinX(minx), 
        mMinY(miny), 
        mMaxX(maxx), 
        mMaxY(maxy)
{
    FdoOwsRequest::SetVersion (version? version : FdoWcsXmlGlobals::WcsVersion);
    
    mIdentifier = identifier;

    mStore = store;
    mGridCRS = gridCRS;
    mTimeSequence = timeSequence;
}

FdoWcsGetCoverage* FdoWcsGetCoverage::Create (FdoString* identifier, FdoString* format, 
                                                FdoString* srsName, 
                                                FdoSize height,
									            FdoSize width,
		                                        FdoDouble minx, 
		                                        FdoDouble miny, 
		                                        FdoDouble maxx, 
		                                        FdoDouble maxy,
                                              FdoBoolean store, 
                                              FdoWcsGridCRS* gridCRS, 
                                              FdoWcsTimeSequence* timeSequence, 
                                              /*TODO: RangeSubSet*/
                                              FdoString* version)
{
    VALIDATE_ARGUMENT(identifier);
    VALIDATE_ARGUMENT(format);


	return new FdoWcsGetCoverage(identifier, format,srsName,height, width,minx, miny, maxx, maxy,store, gridCRS, timeSequence, version);
}

FdoWcsGetCoverage::~FdoWcsGetCoverage ()
{
}

FdoStringP FdoWcsGetCoverage::EncodeKVP()
{
	// For common request, version and service
    FdoStringP ret = FdoOwsRequest::EncodeKVP();

    // Add "identifier" parameter in the request
    ret += FdoOwsGlobals::And;
    ret += FdoWcsXmlGlobals::WcsRequestIdentifier;
    ret += FdoOwsGlobals::Equal;
    ret += mIdentifier;

    // Add "bounding box" parameters in the request
    ret += FdoOwsGlobals::And;
    ret += FdoWcsXmlGlobals::WcsRequestBBOX;
    ret += FdoOwsGlobals::Equal;
    
    //wchar_t minXStr[STRING_FORMAT_BUFFER_LENGTH];
    //wchar_t minYStr[STRING_FORMAT_BUFFER_LENGTH];
    //wchar_t maxXStr[STRING_FORMAT_BUFFER_LENGTH];
    //wchar_t maxYStr[STRING_FORMAT_BUFFER_LENGTH];

    //FdoCommonStringUtil::FormatDouble(mBBox->GetMinX(), minXStr, STRING_FORMAT_BUFFER_LENGTH);
    //FdoCommonStringUtil::FormatDouble(mBBox->GetMinY(), minYStr, STRING_FORMAT_BUFFER_LENGTH);
    //FdoCommonStringUtil::FormatDouble(mBBox->GetMaxX(), maxXStr, STRING_FORMAT_BUFFER_LENGTH);
    //FdoCommonStringUtil::FormatDouble(mBBox->GetMaxY(), maxYStr, STRING_FORMAT_BUFFER_LENGTH);

    //FdoStringP crs = mBBox->GetCRS();

    FdoStringP bbox = FdoStringP::Format(L"%lf,%lf,%lf,%lf,%ls", 
        mMinX, mMinY,
        mMaxX, mMaxY,
        mSrsName);
    ret += bbox;

	// Add "FORMAT" in the request
	ret += FdoOwsGlobals::And;
	ret += FdoWcsXmlGlobals::WcsRequestFormat;
	ret += FdoOwsGlobals::Equal;
	ret += mFormat; // Don't escape, even though there is a "/", ErMapper services don't like this

	// Add the "HEIGHT" and "WIDTH" parameters
	if (mHeight > 0 && mWidth > 0)
	{
		ret += FdoOwsGlobals::And;
		ret += FdoWcsXmlGlobals::WcsRequestHeight;
		ret += FdoOwsGlobals::Equal;
		ret += FdoStringP::Format(L"%d", mHeight);
		ret += FdoOwsGlobals::And;
		ret += FdoWcsXmlGlobals::WcsRequestWidth;
		ret += FdoOwsGlobals::Equal;
		ret += FdoStringP::Format(L"%d", mWidth);
	}
    
    // Translate to a valid HTTP request
    // NOTE: Currently, this is mainly for handling the blank space in "FORMAT"
    ret = ret.Replace(L" ", L"%20"); 

#ifdef _DEBUG
	wofstream outFile("c:\\requestTest.txt");
	outFile << ret << endl;
	outFile.close();
#endif

	return ret;
}

FdoStringP FdoWcsGetCoverage::EncodeXml()
{
	// TODO: shall we support this?
	return L"";
}